cmake_minimum_required(VERSION 3.10)
project(ResourceOptical)

# 查找CUDA工具包
set(BUILD_AI_PLATFORM_SDK "NPU")
#set_property(CACHE BUILD_AI_PLATFORM_SDK PROPERTY STRINGS "CPU" "GPU" "NPU" "NPU-ROCKCHIP" "XPU-PADDLE" )

# 设置源文件目录
set(CPU_SRC_DIR ${CMAKE_SOURCE_DIR}/src/resource_cpu)
set(GPU_SRC_DIR ${CMAKE_SOURCE_DIR}/src/resource_gpu)
set(NPU_SRC_DIR ${CMAKE_SOURCE_DIR}/src/resource_npu)

# 设置头文件目录
include_directories(${CMAKE_SOURCE_DIR}/include)

# 定义CPU、GPU和NPU源文件
file(GLOB CPU_SOURCES ${CPU_SRC_DIR}/*.cpp)
file(GLOB GPU_SOURCES ${GPU_SRC_DIR}/*.cpp)
file(GLOB NPU_SOURCES ${NPU_SRC_DIR}/*.cpp)

# 创建resource_optical_common库，并确保使用-fPIC
add_library(resource_optical_common STATIC ${CMAKE_SOURCE_DIR}/src/common.cpp)
set_property(TARGET resource_optical_common PROPERTY POSITION_INDEPENDENT_CODE ON)

# 根据条件选择性地创建和配置不同的平台库

if("GPU" IN_LIST BUILD_AI_PLATFORM_SDK)
    message(STATUS "CUDA found, enabling GPU support.")
    set(TARGET_LIB resource_optical_gpu)
    add_library(resource_optical_gpu SHARED ${GPU_SOURCES} ${CPU_SOURCES})
    target_link_libraries(resource_optical_gpu PRIVATE resource_optical_common)
    target_compile_definitions(resource_optical_gpu PRIVATE USE_GPU)
    set_property(TARGET resource_optical_gpu PROPERTY POSITION_INDEPENDENT_CODE ON)
    message("The engine compile platform include: GPU.")
else()
    message(STATUS "CUDA not found, falling back to NPU or CPU support.")
endif()

if("CPU" IN_LIST BUILD_AI_PLATFORM_SDK)
    set(TARGET_LIB resource_optical_cpu)
    add_library(resource_optical_cpu SHARED ${CPU_SOURCES})
    target_link_libraries(resource_optical_cpu PRIVATE resource_optical_common)
    target_compile_definitions(resource_optical_cpu PRIVATE USE_CPU)
    set_property(TARGET resource_optical_cpu PROPERTY POSITION_INDEPENDENT_CODE ON)
    message("The engine compile platform include: CPU.")
endif()

if("NPU" IN_LIST BUILD_AI_PLATFORM_SDK)
    set(TARGET_LIB resource_optical_npu)
    add_library(resource_optical_npu SHARED ${NPU_SOURCES} ${CPU_SOURCES})
    target_link_libraries(resource_optical_npu PRIVATE resource_optical_common)
    target_compile_definitions(resource_optical_npu PRIVATE USE_NPU)
    set_property(TARGET resource_optical_npu PROPERTY POSITION_INDEPENDENT_CODE ON)
    message("The engine compile platform include: NPU.")
endif()

# 添加主程序或库的源文件
set(MAIN_SOURCES 
    ${CMAKE_SOURCE_DIR}/main.cpp
)

# 创建最终的resource_optical.so
add_library(resource_optical SHARED ${MAIN_SOURCES})

# 链接所选的库
target_link_libraries(resource_optical PRIVATE ${TARGET_LIB})

add_executable(test_resource_optical ${MAIN_SOURCES})
target_link_libraries(test_resource_optical PRIVATE ${TARGET_LIB})

if("GPU" IN_LIST BUILD_AI_PLATFORM_SDK)
    target_compile_definitions(test_resource_optical PRIVATE USE_GPU)
elseif("NPU" IN_LIST BUILD_AI_PLATFORM_SDK)
    target_compile_definitions(test_resource_optical PRIVATE USE_NPU)
else() # 默认是CPU
    target_compile_definitions(test_resource_optical PRIVATE USE_CPU)
endif()


# 创建软连接
if("GPU" IN_LIST BUILD_AI_PLATFORM_SDK)
add_custom_command(TARGET resource_optical POST_BUILD
                       COMMAND ${CMAKE_COMMAND} -E create_symlink 
                       $<TARGET_FILE_NAME:resource_optical_gpu> 
                       ${CMAKE_BINARY_DIR}/resource_optical.so)
elseif("NPU" IN_LIST BUILD_AI_PLATFORM_SDK)
    add_custom_command(TARGET resource_optical POST_BUILD
                       COMMAND ${CMAKE_COMMAND} -E create_symlink 
                       $<TARGET_FILE_NAME:resource_optical_npu> 
                       ${CMAKE_BINARY_DIR}/resource_optical.so)
else()
    add_custom_command(TARGET resource_optical POST_BUILD
                       COMMAND ${CMAKE_COMMAND} -E create_symlink 
                       $<TARGET_FILE_NAME:resource_optical_cpu> 
                       ${CMAKE_BINARY_DIR}/resource_optical.so)
endif()

# 输出信息以便确认配置
message(STATUS "Linking against: ${TARGET_LIB}")

# 如果有需要，可以设置输出路径
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)